﻿using NetworkListManagerSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NETWORKLIST;

namespace VideoStream.Net
{
    public class NetworkManager : IDisposable
    {
        public static NetworkManager Instance;

        //public delegate void NetConnectedHandler(bool connected);
        //public delegate void NetConnectedToInternetHandler(bool connected);
        public delegate void NetWorkChangedHandler();

        //public event NetConnectedHandler NetDisconnectedEvent;
        //public event NetConnectedToInternetHandler NetDisconnectedToInternetEvent;
        //public event NetWorkChangedHandler NetWorkChangedEvent;

        public event ConnectedToInternetHandler ConnectedToInternetEvent
        {
            add { AppNetworkListUser.instance.ConnectedToInternetEvent += value; }
            remove { AppNetworkListUser.instance.ConnectedToInternetEvent -= value; }
        }
        public event ConnectedHandler ConnectedEvent
        {
            add { AppNetworkListUser.instance.ConnectedEvent += value; }
            remove { AppNetworkListUser.instance.ConnectedEvent -= value; }
        }

        public event NetWorkChangedHandler NetWorkChangedEvent;

        public List<INetwork> NetworkList => AppNetworkListUser.instance.NLM.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_ALL).Cast<INetwork>().ToList();

        public List<INetwork> NetworkConnectedList => NetworkList.Where(o => o.IsConnected).ToList();

        public List<INetwork> NetworkConnectedToInternetList => NetworkList.Where(o => o.IsConnectedToInternet).ToList();

        public bool IsConnectedToInternet
        {

            get { return NetworkConnectedToInternetList?.Count > 0; }
        }

        public bool IsConnected
        {
            get { return NetworkConnectedList?.Count > 0; }
        }

        private INetwork curNetwork;
        private INetwork tmpNetwork;

        //private bool isConnectedToInternet = false;
        //private bool isConnected = false;
        private bool NetworkChecking = false;

        public NetworkManager()
        {
            if (Instance == null)
            {
                Instance = this;
            }
            else if (Instance != this)
            {
                Console.WriteLine(">>>> NetworkManager was Exits! <<<<");
                return;
            }

            AppNetworkListUser.Init(true);
            NetworkChangedChecking();
        }

        private async void NetworkChangedChecking()
        {
            try
            {
                NetworkChecking = true;
                while (NetworkChecking)
                {
                    tmpNetwork = NetworkConnectedToInternetList?.FirstOrDefault();
                    if (tmpNetwork == null)
                    {
                        await Task.Delay(3000);
                        continue;
                    }
                    if (curNetwork == null & tmpNetwork != null)
                    {
                        curNetwork = tmpNetwork;
                    }
                    if (curNetwork.GetNetworkId() != tmpNetwork.GetNetworkId())
                    {
                        curNetwork = tmpNetwork;
                        NetWorkChangedEvent?.Invoke();
                    }

                    await Task.Delay(170);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void Dispose()
        {
            NetworkChecking = false;
        }
    }
}
